#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb
import demjson
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

conn= MySQLdb.connect(
        host='192.168.145.44',
        port = 5506,
        user='root',
        passwd='mysql',
        # db ='contract_product_2017-8-28',
        db ='contract_product-2017-9-9',
        charset="utf8"
        )
cur = conn.cursor()


def generate_namedtuple(cur):
    from collections import namedtuple
    fieldnames = [d[0].lower() for d in cur.description]
    Record = namedtuple('Record', fieldnames)
    rows = cur.fetchall()
    if not rows:return
    else:
        return map(Record._make, rows)

reCount = cur.execute("select * from yx_contract")

for k in generate_namedtuple(cur):

    sql_update = " update yx_contract set "
    # json = '{"channel":"供应商渠道修改","channelId":"402811a858fc81e10158fca701fd0006","contractAmount":"67676","executeEndDate":"2017-03-08 ","executeStartDate":"2017-02-24 ","finalChannel":"供应商渠道修改","finalChannelId":"402811a858fc81e10158fca701fd0006","id":"402811a8591f56bd0159204c871d004b","isNormal":"是","relContractCode":"","signedCompany":"鑫车投资（上海）有限公司","signedDept":"易鑫资本-人力资源中心-招聘交付组","signedDeptId":"1000002","signedUser":"陈师","signedUserId":"chenshi"}'
    json = k.contract_info
    text = demjson.decode(json)
    print  text
    if 'channel' in text :
        channel = text['channel']
        text.pop('channel')
        sql_update += " channel_name = '"+ channel +"',"
    if 'channelId' in text :
        channelId = text['channelId']
        text.pop('channelId')
        sql_update += " channel_id = '"+ channelId +"',"
    if 'contractAmount' in text :
        contractAmount = text['contractAmount']
        text.pop('contractAmount')
        sql_update += " contract_amount = '"+ contractAmount +"',"

    if 'executeEndDate' in text :
        executeEndDate = text['executeEndDate']
        text.pop('executeEndDate')
        sql_update += " execute_end_date = '"+ executeEndDate +"',"

    if 'executeStartDate' in text :
        executeStartDate = text['executeStartDate']
        text.pop('executeStartDate')
        sql_update += " execute_start_date = '" + executeStartDate + "',"
    # if 'id' in text :
    #     id = text['id']
    #     text.pop('id')
    if 'incomeSureType' in text :
        incomeSureType = text['incomeSureType']
        text.pop('incomeSureType')
        sql_update += " income_sure_type = '" + incomeSureType + "',"
    if 'incomeType' in text :
        incomeType = text['incomeType']
        text.pop('incomeType')
        sql_update += " income_type = '"+ incomeType +"',"

    if 'isNormal' in text :
        isNormal = text['isNormal']
        text.pop('isNormal')
        sql_update += " is_normal = '"+ isNormal +"',"

    if 'isReturn' in text :
        isReturn = text['isReturn']
        text.pop('isReturn')
        sql_update += " is_return = '"+ isReturn +"',"

    if 'relContractCode' in text :
        relContractCode = text['relContractCode']
        text.pop('relContractCode')
        sql_update += " rel_contract_code = '"+ relContractCode +"',"

    if 'signedCompany' in text :
        signedCompany = text['signedCompany']
        text.pop('signedCompany')
        sql_update += " signed_company = '"+ signedCompany +"',"

    if 'signedDept' in text :
        signedDept = text['signedDept']
        text.pop('signedDept')
        sql_update += " signed_dept = '"+ signedDept +"',"

    if 'signedDeptId' in text :
        signedDeptId = text['signedDeptId']
        text.pop('signedDeptId')
        sql_update += " signed_dept_id = '"+ signedDeptId +"',"

    if 'signedUser' in text :
        signedUser = text['signedUser']
        text.pop('signedUser')
        sql_update += " signed_user = '"+ signedUser +"',"

    if 'signedUserId' in text :
        signedUserId = text['signedUserId']
        text.pop('signedUserId')
        sql_update += " signed_user_id = '"+ signedUserId +"',"

    json_filter = demjson.encode(text,'utf-8')
    print json_filter

    sql_update += " contract_info = '"+ json_filter +"'"


    # sql_update = sql_update.rstrip(',')
    sql_update += " where ID = '"+k.id+"'"
    # 修改查询条件的数据
    result = cur.execute(sql_update)
    print  sql_update
    print 'update result===> '+ str(result)

cur.close()
conn.commit()
conn.close()